%% Generate two random target sequences!
ambiguities = 2; % nr of ambiguities
targets = 10; % nr of targets
p_amb = 0.2;

% This could be a function:
% targetList = f1(targets,ambiguities,p_amb) 
s1TargetList = zeros(targets, ambiguities);
randomTargetList = randperm(max(targets,ambiguities));
n = 1;
for row = 1:targets
  for col = 1:ambiguities
    if (n > length(randomTargetList))
      break;
    end
    if (col == 1) || (rand(1) < p_amb)
      s1TargetList(row, col) = randomTargetList(n);
      n = n + 1;
    else
      break;
    end
  end
end
% f1()

% This could be a function:
% targetList = f1(targets,ambiguities,p_amb) 
s2TargetList = zeros(targets, ambiguities);
randomTargetList = randperm(max(targets,ambiguities));
n = 1;
for row = 1:targets
  for col = 1:ambiguities
    if (n > length(randomTargetList))
      break;
    end
    if (col == 1) || (rand(1) < p_amb)
      s2TargetList(row, col) = randomTargetList(n);
      n = n + 1;
    else
      break;
    end
  end
end
% f1()

randomTargetList = randperm(max(targets,ambiguities));

%% Schedule targets build IA network first and the set equals
scheduledTargetList1 = [];
ia_network = IA_Network_Empty();
prev_nodes = [];
curr_node = 1;

for row = 1:targets
  temp_nodes = [];
  for col = 1:ambiguities
    target_id = s1TargetList(row, col);
    if (target_id ~= 0)
      ia_interval = IA_Interval(sprintf('S1_T%i', target_id), target_id);
      ia_network = IA_Network_Add_Interval(ia_network, ia_interval);
      for node = 1:length(prev_nodes)
        target_id2 = prev_nodes(node);
        if (target_id2 ~= 0)
          [ia_network, consistent] = IA_Network_Path_Consistency_Hogge(ia_network, prev_nodes(node), curr_node, IA_Relation_Less());
        end
      end 
      temp_nodes = [temp_nodes curr_node]; %#ok<AGROW>
      curr_node = curr_node + 1;
    end
  end
  prev_nodes = temp_nodes;
end

prev_nodes = [];

for row = 1:targets
  temp_nodes = [];
  for col = 1:ambiguities
    target_id = s2TargetList(row, col);
    if (target_id ~= 0)
      ia_interval = IA_Interval(sprintf('S2_T%i', target_id), target_id);
      ia_network = IA_Network_Add_Interval(ia_network, ia_interval);
      for node = 1:length(prev_nodes)
        target_id2 = prev_nodes(node);
        if (target_id2 ~= 0)
          [ia_network, consistent] = IA_Network_Path_Consistency_Hogge(ia_network, prev_nodes(node), curr_node, IA_Relation_Less());
        end
      end 
      temp_nodes = [temp_nodes curr_node]; %#ok<AGROW>
      curr_node = curr_node + 1;
    end
  end
  prev_nodes = temp_nodes;
end

[s1nrTargets, s1olTargets, s1olBins] = ST_OrderedTargetList(s1TargetList);
[s3nrTargets, s2olTargets, s2olBins] = ST_OrderedTargetList(s2TargetList);

for target = 1:length(randomTargetList)
  target_id = randomTargetList(target);
  node_i = ST_FindTargetId(s1olTargets, target_id);
  node_j = ST_FindTargetId(s2olTargets, target_id) + s1nrTargets;
  [ia_net_temp, consistent] = IA_Network_Path_Consistency_Hogge(ia_network, node_i, node_j, IA_Relation_Equals());
  if consistent
    scheduledTargetList1 = [scheduledTargetList1 target_id]; %#ok<AGROW>
    ia_network = ia_net_temp;
  end
end

%% Schedule targest build IA network as we go (one interval per target per sensor)
scheduledTargetList2 = [];
ia_network = IA_Network_Empty();
for target = 1:length(randomTargetList)
  target_id = randomTargetList(target);
  ia_interval1 = IA_Interval(sprintf('S1_T%i', target_id), target_id);
  ia_interval2 = IA_Interval(sprintf('S2_T%i', target_id), target_id);
  ia_nodes = IA_Network_Nodes(ia_network);
  ia_net_temp = IA_Network_Add_Interval(ia_network, ia_interval1);
  ia_net_temp = IA_Network_Add_Interval(ia_net_temp, ia_interval2);
  [ia_net_temp, consistent] = IA_Network_Path_Consistency_Hogge(ia_net_temp, ia_nodes+1, ia_nodes+2, IA_Relation_Equals());
  if consistent
    for node = 1:ia_nodes
      node_id = ia_network.nodes(node).duration;
      if node_id ~= target_id
        [t_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == target_id));
        [n_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == node_id));

        if n_row1 < t_row1
          relation = IA_Relation_Less();
        elseif n_row1 == t_row1
          relation = IA_Relation_Dummy();
        else
          relation = IA_Relation_Greater();
        end

        [ia_net_temp, consistent] = IA_Network_Path_Consistency_Hogge(ia_net_temp, node, ia_nodes+1, relation);
        if ~consistent
          break;
        end

        [t_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == target_id));
        [n_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == node_id));

        if n_row2 < t_row2
          relation = IA_Relation_Less();
        elseif n_row2 == t_row2
          relation = IA_Relation_Dummy();
        else
          relation = IA_Relation_Greater();
        end

        [ia_net_temp, consistent] = IA_Network_Path_Consistency_Hogge(ia_net_temp, node, ia_nodes+2, relation);
        if ~consistent
          break;
        end
      end
    end
    if consistent
      ia_network = ia_net_temp;
      scheduledTargetList2 = [scheduledTargetList2 target_id]; %#ok<AGROW>
    end
  end
end

%% Schedule targets build IA network as we go (one interval per target)
scheduledTargetList3 = [];
ia_network = IA_Network_Empty();
for target = 1:length(randomTargetList)
  target_id = randomTargetList(target);
  ia_interval = IA_Interval(sprintf('T%i', target_id), target_id);
  ia_nodes = IA_Network_Nodes(ia_network);
  consistent = 1;
  ia_net_temp = IA_Network_Add_Interval(ia_network, ia_interval);
  for node = 1:ia_nodes
    node_id = ia_network.nodes(node).duration;
    if node_id ~= target_id
      [t_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == target_id));
      [n_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == node_id));
      
      if n_row1 < t_row1
        relation = IA_Relation_Less();
      elseif n_row1 == t_row1
        relation = IA_Relation_Dummy();
      else
        relation = IA_Relation_Greater();
      end
      
      [t_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == target_id));
      [n_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == node_id));
      
      if n_row2 < t_row2
        relation = IA_Relation_Intersect(relation, IA_Relation_Less());
      elseif n_row2 == t_row2
        relation = IA_Relation_Intersect(relation, IA_Relation_Dummy());
      else
        relation = IA_Relation_Intersect(relation, IA_Relation_Greater());
      end
      
      if IA_Relation_Is_Null(relation)
        consistent = 0;
        break;
      end
      
      [ia_net_temp, consistent] = IA_Network_Path_Consistency_Hogge(ia_net_temp, node, ia_nodes+1, relation);
      if ~consistent
        break;
      end
    end
  end
  if consistent
    ia_network = ia_net_temp;
    scheduledTargetList3 = [scheduledTargetList3 target_id]; %#ok<AGROW>
  end
end

t = 1;